import Api from '../../constants/Api'
import axios from '@/axios'

export default {
  state: {
    token: '',
    isCollapse: false,
    navTree: []
  },
  mutations: {
    // 菜单收缩
    collapse(state) {
      state.isCollapse = !state.isCollapse
    }
  },
  actions: {
    // 获取全局设置
    getGlobal({commit}, that) {
      return axios({
        method: 'post',
        url: Api.getSettingData,
        data: {
          status: 'S'
        }
      }).then(res => {
        if (res.code === 20001) {
          localStorage.removeItem('baseUrl')
          let url = res.data.minioFileServiceURL.value
          that.$global.setBaseUrl(url)
          localStorage.setItem('baseUrl', url)
        }
      }).catch(err => {
        return err
      })
    },
    // 获取token
    getToken({state}, that) {
      let url = Api.getToken.replace('{0}', encodeURI(that.ruleForm.username))
      url = url.replace('{1}', that.ruleForm.password)
      url = encodeURI(url)
      return axios({
        method: 'post',
        url: url,
        auth: {
          username: 'job-cli',
          password: '123456'
        }
      }).then(res => {
        localStorage.setItem('token', res.token_type + ' ' + res.access_token)
        localStorage.setItem('userInfo', JSON.stringify(res.principal))
        that.$global.setUserInfo(res.principal)
        return res
      }).catch(err => {
        that.isLoading = false
        that.$message.error(err.data.msg)
      })
    },
    // 获取左侧菜单
    getNavTree({state}, that) {
      return axios({
        method: 'get',
        url: Api.getNavTree
      }).then(res => {
 
        state.navTree = res.data
        that.loading = false
      }).catch(err => {
        that.loading = false
        that.$message.error(err.data.msg)
      })
    }
  }
}
